<template>
	<div>
		<BreadCrumb pathOne="内容管理" pathTwo="文章管理" ></BreadCrumb>
		<div>
			<el-form :inline="true">
				<el-form-item label="文章名" size="large">
					<el-input clearable size="large" v-model="searchName" style="width: 240px" placeholder="请输入文章名" />
				</el-form-item>
				<el-form-item label="咨询师作者" size="large">
					<el-select @clear="counselorId=''" clearable v-model="counselorId" placeholder="无" size="large"
						style="width: 240px">
						<el-option v-for="(item,index) in AllAuthor" :value="item.id" :label="item.counselorName" />
					</el-select>
				</el-form-item>
				<el-form-item label="所属分类" size="large">
					<el-select @clear="articleClassifyId=''" clearable v-model="articleClassifyId" placeholder="无"
						size="large" style="width: 240px">
						<el-option v-for="(item,index) in AllClassify" :value="item.id"
							:label="item.articleClassifyName" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="query" size="large">搜索</el-button>
					<el-button type="success" @click="batchRecover" size="large">批量恢复</el-button>
					<el-button type="danger" size="large" @click="batchDelete">批量删除</el-button>
				</el-form-item>
			</el-form>

		</div>
		<div>
			<el-alert title="回收站中的文件只能保存15天" type="warning" show-icon :closable="false" center/>
			<el-table :data="tableData" style="width: 100%;" border
				:header-cell-style="{textAlign: 'center'}" :cell-style="{ textAlign: 'center' }" @selection-change="handleSelectionChange">
				
				<el-table-column type="selection" width="40" />
				<el-table-column fixed prop="articleId" label="id" width="50" />
				<el-table-column prop="articleName" label="文章名" width="110" show-overflow-tooltip />
				<el-table-column prop="articleIntro" label="文章简介" width="110" show-overflow-tooltip />
				<el-table-column prop="counselor.counselorName" label="咨询师名" width="90" />
				<el-table-column label="缩略图" width="120">
					<template #default="scope">
						<!-- <img :src="scope.row.counselor.counselorImg" alt="缩略图"> -->
						<el-avatar shape="square" size="large" src="" />
					</template>
				</el-table-column>

				<el-table-column prop="pageviews" label="阅读量" width="70" />
				<el-table-column prop="artClassify.articleClassifyName" label="所属分类" width="90" />
				<el-table-column prop="deleteTime" label="删除时间" width="180" />
				<el-table-column label="审核状态" width="110">
					<!-- 0是审核中，1是通过，2是不通过 -->
					<template #default="scope">
						<el-tag type="warning" v-if="scope.row.status==0">审核中</el-tag>
						<el-tag type="success" v-else-if="scope.row.status==1">已审核</el-tag>
						<el-tag type="danger" v-else>审核未通过</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="状态" width="70">
					<!-- 0是审核中，1是通过，2是不通过 -->
						<el-tag type="danger">停用</el-tag>
				</el-table-column>
				<el-table-column fixed="right" label="操作" width="270">
					<template #default="scope">
						<el-button size="small" type="success" @click="add(scope.row.articleId)">恢复</el-button>
						<el-button size="small" type="danger" @click="remove(scope.row.articleId)">彻底删除</el-button>
						<el-button size="small" type="primary" @click="searchArticleDetails(scope.row.articleId)">查看详情内容</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[5, 10,15, 20]"
			:size="size" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
			@current-change="handleCurrentChange" />
		
		<el-dialog v-model="dialogArticleDetails" title="内容详情" width="700" style="margin-bottom: 60px;"
			@close="handleClose">
			<el-form-item label="" label-width="auto" label-position='top'>
					<QuillEditor :value="form.articleDetails" @updateValue="getMsg"></QuillEditor>
			</el-form-item>
			</el-dialog>
	</div>
</template>

<script setup>
	import {
		getCurrentInstance,
		onMounted,
		reactive,
		ref
	} from 'vue'
	import {
		ElMessage,
		ElMessageBox
	} from 'element-plus'
	import { Plus } from '@element-plus/icons-vue'
	import BreadCrumb from "../../components/BreadCrumb.vue"
	import QuillEditor from "../../components/QuillEditor.vue"
	const getMsg = (val) => {
		// console.log('form.value.articleDetails',form.value.articleDetails);
	form.value.articleDetails = val
	console.log('监听的值：',val);
	}
	const AllAuthor = ref([])
	const AllClassify = ref([])
	const axios = getCurrentInstance().appContext.config.globalProperties.axios;
	const getAllAuthor = () => {
		axios.get(
			`counselor/page?id=&pageNum=&pageSize=`,
		).then(resp => {
			console.log('全部作者：', resp);
			AllAuthor.value = resp.data.list
		})
	}
	const getAllClassify = () => {
		axios.get(
			`artClassify/page?id=&pageNum=&pageSize=`,
		).then(resp => {
			console.log('全部分类：', resp);
			AllClassify.value = resp.data.list
		})
	}
	const tableData = ref([])
	
	const counselorId = ref('')
	const articleClassifyId = ref('')
	const query = () => {
		axios.get(
			`recycle/page?article_id=&articleName=${searchName.value}&counselorId=${counselorId.value}&articleClassifyId=${articleClassifyId.value}&pageNum=${currentPage.value}&pageSize=${pageSize.value}`,
		).then(resp => {
			console.log('数据查询请求：', resp);
			tableData.value = resp.data.list
			total.value = resp.data.total
			// console.log("表单数据：", tableData.value);
		})
	}
	const currentPage = ref(1) // 当前页
	const pageSize = ref(10) // 每页显示条目数
	const size = 'default' // 分页大小
	const total = ref(0)
	const handleSizeChange = (val) => {
		pageSize.value = val;
		query();
	}
	const handleCurrentChange = (val) => {
		currentPage.value = val
		query();
	}
	const searchName = ref('')
	const dialogArticleDetails=ref(false)
	// const articleClassifyName = ref('')
	const handleClose = () => {
		form.value ={
			artClassify: {
				articleClassifyName: "",
				del: 0,
				id: ""
			},
			articleClassifyId: 17,//分类
			articleCreatime: "",
			articleDetails: ' ',//<p>新增文章内容详情</p>
			articleId: "",
			articleIntro: "",//新增简介
			articleName: "",//新增文章
			articleView: "",
			counselor: {
				counselorName: ""
			},
			counselorName: "",
			deleteTime:'',
			counselorId: 83,//咨询作者
			del: '0',
			pageviews: "",
			status: "",
		}
	}
	const searchArticleDetails=(id)=>{
		dialogArticleDetails.value = true
		if (id == '') {
			handleClose();
		
		} else {
			axios.get(
				`recycle/${id}`,
			).then(resp => {
				form.value = resp.data
				console.log('编辑查询：', resp);
			})
		}
	}
	const form = ref({
		artClassify: {
			articleClassifyName: "",
			del: 0,
			id: ""
		},
		articleClassifyId: 17,//分类
		articleCreatime: "",
		articleDetails: ' ',//<p>新增文章内容详情</p>
		articleId: "",
		articleIntro: "",//新增简介
		articleName: "",//新增文章
		articleView: "",
		counselor: {
			counselorName: ""
		},
		counselorName: "",
		deleteTime:'',
		counselorId: 83,//咨询作者
		del: '0',
		pageviews: "",
		status: "",
	})
const add = (id) => {
		console.log('当前行的数据', id);
		ElMessageBox.confirm(
				'此操作将恢复选择的文件, 是否继续?',
				'提示', {
					confirmButtonText: '确认',
					cancelButtonText: '取消',
					type: 'warning',
				}
			)
			.then(() => {
				axios.put(`restore/${id}`).then((resp) => {
					console.log('恢复后：', resp);
					if (resp.code == 200) {
						ElMessage({
							message: '恢复成功',
							type: 'success',
						})
					} else {
						ElMessage.error('恢复失败')
					}
				}).finally(() => {
					query();
				});
		
			})
			.catch(() => {
				ElMessage({
					type: 'info',
					message: '取消删除',
				})
			})
	}
	const remove = (id) => {
		console.log('当前行的数据', id);
		ElMessageBox.confirm(
				'此操作将永久删除该文件, 是否继续?',
				'提示', {
					confirmButtonText: '确认',
					cancelButtonText: '取消',
					type: 'warning',
				}
			)
			.then(() => {
				axios.delete(`recycle/${id}`).then((resp) => {
					console.log('删除后：', resp);
					if (resp.code == 200) {
						ElMessage({
							message: '删除成功',
							type: 'success',
						})
					} else {
						ElMessage.error('删除失败')
					}
				}).finally(() => {
					query();
				});

			})
			.catch(() => {
				ElMessage({
					type: 'info',
					message: '取消删除',
				})
			})

	}
	const ids = ref('')
	const handleSelectionChange = (selection) => {
		ids.value = selection.map(item => item.articleId).join(',')
		console.log('批量删除的ids为:', ids.value)
	}
	const batchRecover = () => {
		if (!ids.value) {
			ElMessage.info('请选择要恢复的数据')
			return
		}
		ElMessageBox.confirm('此操作将恢复选择的文件, 是否继续?', '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning'
		}).then(() => {
			axios.put(`restore?ids=${ids.value}`).then(res => {
				ElMessage.success('恢复成功')
				query();
			})
		}).catch(() => {
			ElMessage.info('已取消恢复')
		}).finally(() => {
			query();
		});
	}
	const batchDelete = () => {
		if (!ids.value) {
			ElMessage.info('请选择要删除的数据')
			return
		}
		ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning'
		}).then(() => {
			axios.delete(`recycle?ids=${ids.value}`).then(res => {
				ElMessage.success('删除成功')
				query();
			})
		}).catch(() => {
			ElMessage.info('已取消删除')
		}).finally(() => {
			query();
		});
	}

	onMounted(() => {
		query()
		getAllAuthor()
		getAllClassify()
	})
</script>

<style scoped>
	.avatar-uploader .avatar {
	  width: 178px;
	  height: 178px;
	  display: block;
	}
	.avatar-uploader .el-upload {
		border: 1px dashed var(--el-border-color);
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		transition: var(--el-transition-duration-fast);
	}
	
	.avatar-uploader .el-upload:hover {
		border-color: var(--el-color-primary);
	}
	
	.el-icon.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		text-align: center;
	}
</style>